package com.bloveoacom.weichi.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bloveoacom.base.BaseActivity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.MessageModel;
import com.bloveoacom.http.RxBus;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.UserBean;
import com.bloveoacom.weichi.contract.LoginContract;
import com.bloveoacom.weichi.model.LoginModel;
import com.bloveoacom.weichi.presenter.LoginPresenter;
import com.bloveoacom.weight.DelEditText;
import com.bloveoacom.weight.DrawableCenterTextView;
import com.bloveoacom.weight.LlkjToolBar;
import com.jakewharton.rxbinding2.widget.RxTextView;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/20
 * desc  :
 */
public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements LoginContract.View {
    @BindView(R.id.title_bar)
    LlkjToolBar titleBar;
    @BindView(R.id.etLoginName)
    DelEditText etLoginName;
    @BindView(R.id.btnGetCode)
    TextView btnGetCode;
    @BindView(R.id.etLoginPwd)
    DelEditText etLoginPwd;
    @BindView(R.id.btnTvLogin)
    DrawableCenterTextView btnTvLogin;
    @BindView(R.id.tv_agree)
    TextView tvAgree;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_login_code;
    }

    @Override
    public void initView() {
        titleBar.setToolBar("",
                true, "", R.drawable.ic_back, false, "", LlkjToolBar.isNull);
        titleBar.setTitleBg(R.color.white);
    }

    @Override
    protected View getViewSetTB() {
        return titleBar;
    }

    @Override
    public boolean isSetBarColor() {
        return false;
    }

    @Override
    public void initData() {
        tvAgree.setText(new SpanUtils().append("登录即表示你已阅读，并同意").setForegroundColor(ContextCompat.getColor(mContext,R.color.color_858585)).append("《用户注册协议》").setForegroundColor(ContextCompat.getColor(mContext,R.color.colorPrimary)).create());
    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {
        Observable<CharSequence> etAccountOb = RxTextView.textChanges(etLoginName);
        Observable<CharSequence> etNumberOb = RxTextView.textChanges(etLoginPwd);
        Disposable subscribe = Observable.combineLatest(etAccountOb, etNumberOb, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence charSequence, CharSequence charSequence2) throws Exception {
                return !StringUtils.isEmpty(charSequence) && !StringUtils.isEmpty(charSequence2) ;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                btnTvLogin.setEnabled(aBoolean);
            }
        });
        addDispose(subscribe);

    }

    @Override
    public void loginSuccess(UserBean bean) {
        UserInfoBean.getInstance().setAccess(bean);
        UserInfoBean.getInstance().setLogin(true);
        RxBus.getInstance().send(new MessageModel(MessageModel.TU_TYPE_LOGIN));
        finish();
    }

    @Override
    public void sendCode(String bean) {
        RxHelper.getCode(btnGetCode);
    }

    private String phone,pwd;

    @OnClick({R.id.btnGetCode, R.id.btnTvLogin, R.id.tv_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnGetCode:
                phone =  String.valueOf(etLoginName.getText());
                mPresenter.sendCode(phone,0);
                break;
            case R.id.btnTvLogin:
                phone =  String.valueOf(etLoginName.getText());
                pwd =  String.valueOf(etLoginPwd.getText());
                mPresenter.login(phone,pwd);
                break;
            case R.id.tv_agree:
                ActivityUtil.startActivity(mContext,WebViewActivity.class,"用户协议","0","http://47.105.41.107:8180/file-server/yhxy.html");
                break;
        }
    }
}
